package com.learndemo.mvcpvmdemo.mvp;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.learndemo.mvcpvmdemo.R;
import com.learndemo.mvcpvmdemo.bean.Account;
import com.learndemo.mvcpvmdemo.mvc.MVCModel;

public class MVPActivity extends AppCompatActivity implements View.OnClickListener, IMVPView {
    private TextView mTvResult;
    private EditText mEtAccount;
    private MVCModel mMvcModel; //持有MVCModel引用
    MVPPresenter mvpPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal);
        setTitle("MVPActivity");
        initView();
        mMvcModel = new MVCModel();
         mvpPresenter = new MVPPresenter(this);
    }

    private void initView() {
        mTvResult = findViewById(R.id.tv_result);
        mEtAccount = findViewById(R.id.et_account);
        findViewById(R.id.btn_getAccount).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mvpPresenter.getData(getUserInput());
    }

    @Override
    public String getUserInput() {
        return mEtAccount.getText().toString();
    }

    @Override
    public void showSuccessPage(Account account) {
        mTvResult.setText("用户账号：" + account.getName() + " 用户等级：" + account.getLevel());
    }

    @Override
    public void showFailedPage() {
        mTvResult.setText("获取用户信息失败");
    }
}
